


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > Launcher</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman</a>
</div>

<h1>Coverage Summary for Class: Launcher (nl.tudelft.jpacman)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">Launcher</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/22)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/43)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package nl.tudelft.jpacman;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import java.awt.event.KeyEvent;
<i class="no-highlight">4</i>&nbsp;import java.io.IOException;
<i class="no-highlight">5</i>&nbsp;import java.util.List;
<i class="no-highlight">6</i>&nbsp;
<i class="no-highlight">7</i>&nbsp;import nl.tudelft.jpacman.board.BoardFactory;
<i class="no-highlight">8</i>&nbsp;import nl.tudelft.jpacman.board.Direction;
<i class="no-highlight">9</i>&nbsp;import nl.tudelft.jpacman.game.Game;
<i class="no-highlight">10</i>&nbsp;import nl.tudelft.jpacman.game.GameFactory;
<i class="no-highlight">11</i>&nbsp;import nl.tudelft.jpacman.level.Level;
<i class="no-highlight">12</i>&nbsp;import nl.tudelft.jpacman.level.LevelFactory;
<i class="no-highlight">13</i>&nbsp;import nl.tudelft.jpacman.level.MapParser;
<i class="no-highlight">14</i>&nbsp;import nl.tudelft.jpacman.level.Player;
<i class="no-highlight">15</i>&nbsp;import nl.tudelft.jpacman.level.PlayerFactory;
<i class="no-highlight">16</i>&nbsp;import nl.tudelft.jpacman.npc.ghost.GhostFactory;
<i class="no-highlight">17</i>&nbsp;import nl.tudelft.jpacman.points.PointCalculator;
<i class="no-highlight">18</i>&nbsp;import nl.tudelft.jpacman.points.PointCalculatorLoader;
<i class="no-highlight">19</i>&nbsp;import nl.tudelft.jpacman.sprite.PacManSprites;
<i class="no-highlight">20</i>&nbsp;import nl.tudelft.jpacman.ui.Action;
<i class="no-highlight">21</i>&nbsp;import nl.tudelft.jpacman.ui.PacManUI;
<i class="no-highlight">22</i>&nbsp;import nl.tudelft.jpacman.ui.PacManUiBuilder;
<i class="no-highlight">23</i>&nbsp;
<i class="no-highlight">24</i>&nbsp;/**
<i class="no-highlight">25</i>&nbsp; * Creates and launches the JPacMan UI.
<i class="no-highlight">26</i>&nbsp; * 
<i class="no-highlight">27</i>&nbsp; * @author Jeroen Roosen
<i class="no-highlight">28</i>&nbsp; */
<b class="nc"><i class="no-highlight">29</i>&nbsp;@SuppressWarnings(&quot;PMD.TooManyMethods&quot;)</b>
<b class="nc"><i class="no-highlight">30</i>&nbsp;public class Launcher {</b>
<i class="no-highlight">31</i>&nbsp;
<b class="nc"><i class="no-highlight">32</i>&nbsp;    private static final PacManSprites SPRITE_STORE = new PacManSprites();</b>
<i class="no-highlight">33</i>&nbsp;
<i class="no-highlight">34</i>&nbsp;    public static final String DEFAULT_MAP = &quot;/board.txt&quot;;
<b class="nc"><i class="no-highlight">35</i>&nbsp;    private String levelMap = DEFAULT_MAP;</b>
<i class="no-highlight">36</i>&nbsp;
<i class="no-highlight">37</i>&nbsp;    private PacManUI pacManUI;
<i class="no-highlight">38</i>&nbsp;    private Game game;
<i class="no-highlight">39</i>&nbsp;
<i class="no-highlight">40</i>&nbsp;    /**
<i class="no-highlight">41</i>&nbsp;     * @return The game object this launcher will start when {@link #launch()}
<i class="no-highlight">42</i>&nbsp;     *         is called.
<i class="no-highlight">43</i>&nbsp;     */
<i class="no-highlight">44</i>&nbsp;    public Game getGame() {
<b class="nc"><i class="no-highlight">45</i>&nbsp;        return game;</b>
<i class="no-highlight">46</i>&nbsp;    }
<i class="no-highlight">47</i>&nbsp;
<i class="no-highlight">48</i>&nbsp;    /**
<i class="no-highlight">49</i>&nbsp;     * The map file used to populate the level.
<i class="no-highlight">50</i>&nbsp;     *
<i class="no-highlight">51</i>&nbsp;     * @return The name of the map file.
<i class="no-highlight">52</i>&nbsp;     */
<i class="no-highlight">53</i>&nbsp;    protected String getLevelMap() {
<b class="nc"><i class="no-highlight">54</i>&nbsp;        return levelMap;</b>
<i class="no-highlight">55</i>&nbsp;    }
<i class="no-highlight">56</i>&nbsp;
<i class="no-highlight">57</i>&nbsp;    /**
<i class="no-highlight">58</i>&nbsp;     * Set the name of the file containing this level&#39;s map.
<i class="no-highlight">59</i>&nbsp;     *
<i class="no-highlight">60</i>&nbsp;     * @param fileName
<i class="no-highlight">61</i>&nbsp;     *            Map to be used.
<i class="no-highlight">62</i>&nbsp;     * @return Level corresponding to the given map.
<i class="no-highlight">63</i>&nbsp;     */
<i class="no-highlight">64</i>&nbsp;    public Launcher withMapFile(String fileName) {
<b class="nc"><i class="no-highlight">65</i>&nbsp;        levelMap = fileName;</b>
<b class="nc"><i class="no-highlight">66</i>&nbsp;        return this;</b>
<i class="no-highlight">67</i>&nbsp;    }
<i class="no-highlight">68</i>&nbsp;
<i class="no-highlight">69</i>&nbsp;    /**
<i class="no-highlight">70</i>&nbsp;     * Creates a new game using the level from {@link #makeLevel()}.
<i class="no-highlight">71</i>&nbsp;     *
<i class="no-highlight">72</i>&nbsp;     * @return a new Game.
<i class="no-highlight">73</i>&nbsp;     */
<i class="no-highlight">74</i>&nbsp;    public Game makeGame() {
<b class="nc"><i class="no-highlight">75</i>&nbsp;        GameFactory gf = getGameFactory();</b>
<b class="nc"><i class="no-highlight">76</i>&nbsp;        Level level = makeLevel();</b>
<b class="nc"><i class="no-highlight">77</i>&nbsp;        game = gf.createSinglePlayerGame(level, loadPointCalculator());</b>
<b class="nc"><i class="no-highlight">78</i>&nbsp;        return game;</b>
<i class="no-highlight">79</i>&nbsp;    }
<i class="no-highlight">80</i>&nbsp;
<i class="no-highlight">81</i>&nbsp;    private PointCalculator loadPointCalculator() {
<b class="nc"><i class="no-highlight">82</i>&nbsp;        return new PointCalculatorLoader().load();</b>
<i class="no-highlight">83</i>&nbsp;    }
<i class="no-highlight">84</i>&nbsp;
<i class="no-highlight">85</i>&nbsp;    /**
<i class="no-highlight">86</i>&nbsp;     * Creates a new level. By default this method will use the map parser to
<i class="no-highlight">87</i>&nbsp;     * parse the default board stored in the &lt;code&gt;board.txt&lt;/code&gt; resource.
<i class="no-highlight">88</i>&nbsp;     *
<i class="no-highlight">89</i>&nbsp;     * @return A new level.
<i class="no-highlight">90</i>&nbsp;     */
<i class="no-highlight">91</i>&nbsp;    public Level makeLevel() {
<i class="no-highlight">92</i>&nbsp;        try {
<b class="nc"><i class="no-highlight">93</i>&nbsp;            return getMapParser().parseMap(getLevelMap());</b>
<b class="nc"><i class="no-highlight">94</i>&nbsp;        } catch (IOException e) {</b>
<b class="nc"><i class="no-highlight">95</i>&nbsp;            throw new PacmanConfigurationException(</b>
<b class="nc"><i class="no-highlight">96</i>&nbsp;                    &quot;Unable to create level, name = &quot; + getLevelMap(), e);</b>
<i class="no-highlight">97</i>&nbsp;        }
<i class="no-highlight">98</i>&nbsp;    }
<i class="no-highlight">99</i>&nbsp;
<i class="no-highlight">100</i>&nbsp;    /**
<i class="no-highlight">101</i>&nbsp;     * @return A new map parser object using the factories from
<i class="no-highlight">102</i>&nbsp;     *         {@link #getLevelFactory()} and {@link #getBoardFactory()}.
<i class="no-highlight">103</i>&nbsp;     */
<i class="no-highlight">104</i>&nbsp;    protected MapParser getMapParser() {
<b class="nc"><i class="no-highlight">105</i>&nbsp;        return new MapParser(getLevelFactory(), getBoardFactory());</b>
<i class="no-highlight">106</i>&nbsp;    }
<i class="no-highlight">107</i>&nbsp;
<i class="no-highlight">108</i>&nbsp;    /**
<i class="no-highlight">109</i>&nbsp;     * @return A new board factory using the sprite store from
<i class="no-highlight">110</i>&nbsp;     *         {@link #getSpriteStore()}.
<i class="no-highlight">111</i>&nbsp;     */
<i class="no-highlight">112</i>&nbsp;    protected BoardFactory getBoardFactory() {
<b class="nc"><i class="no-highlight">113</i>&nbsp;        return new BoardFactory(getSpriteStore());</b>
<i class="no-highlight">114</i>&nbsp;    }
<i class="no-highlight">115</i>&nbsp;
<i class="no-highlight">116</i>&nbsp;    /**
<i class="no-highlight">117</i>&nbsp;     * @return The default {@link PacManSprites}.
<i class="no-highlight">118</i>&nbsp;     */
<i class="no-highlight">119</i>&nbsp;    protected PacManSprites getSpriteStore() {
<b class="nc"><i class="no-highlight">120</i>&nbsp;        return SPRITE_STORE;</b>
<i class="no-highlight">121</i>&nbsp;    }
<i class="no-highlight">122</i>&nbsp;
<i class="no-highlight">123</i>&nbsp;    /**
<i class="no-highlight">124</i>&nbsp;     * @return A new factory using the sprites from {@link #getSpriteStore()}
<i class="no-highlight">125</i>&nbsp;     *         and the ghosts from {@link #getGhostFactory()}.
<i class="no-highlight">126</i>&nbsp;     */
<i class="no-highlight">127</i>&nbsp;    protected LevelFactory getLevelFactory() {
<b class="nc"><i class="no-highlight">128</i>&nbsp;        return new LevelFactory(getSpriteStore(), getGhostFactory(), loadPointCalculator());</b>
<i class="no-highlight">129</i>&nbsp;    }
<i class="no-highlight">130</i>&nbsp;
<i class="no-highlight">131</i>&nbsp;    /**
<i class="no-highlight">132</i>&nbsp;     * @return A new factory using the sprites from {@link #getSpriteStore()}.
<i class="no-highlight">133</i>&nbsp;     */
<i class="no-highlight">134</i>&nbsp;    protected GhostFactory getGhostFactory() {
<b class="nc"><i class="no-highlight">135</i>&nbsp;        return new GhostFactory(getSpriteStore());</b>
<i class="no-highlight">136</i>&nbsp;    }
<i class="no-highlight">137</i>&nbsp;
<i class="no-highlight">138</i>&nbsp;    /**
<i class="no-highlight">139</i>&nbsp;     * @return A new factory using the players from {@link #getPlayerFactory()}.
<i class="no-highlight">140</i>&nbsp;     */
<i class="no-highlight">141</i>&nbsp;    protected GameFactory getGameFactory() {
<b class="nc"><i class="no-highlight">142</i>&nbsp;        return new GameFactory(getPlayerFactory());</b>
<i class="no-highlight">143</i>&nbsp;    }
<i class="no-highlight">144</i>&nbsp;
<i class="no-highlight">145</i>&nbsp;    /**
<i class="no-highlight">146</i>&nbsp;     * @return A new factory using the sprites from {@link #getSpriteStore()}.
<i class="no-highlight">147</i>&nbsp;     */
<i class="no-highlight">148</i>&nbsp;    protected PlayerFactory getPlayerFactory() {
<b class="nc"><i class="no-highlight">149</i>&nbsp;        return new PlayerFactory(getSpriteStore());</b>
<i class="no-highlight">150</i>&nbsp;    }
<i class="no-highlight">151</i>&nbsp;
<i class="no-highlight">152</i>&nbsp;    /**
<i class="no-highlight">153</i>&nbsp;     * Adds key events UP, DOWN, LEFT and RIGHT to a game.
<i class="no-highlight">154</i>&nbsp;     *
<i class="no-highlight">155</i>&nbsp;     * @param builder
<i class="no-highlight">156</i>&nbsp;     *            The {@link PacManUiBuilder} that will provide the UI.
<i class="no-highlight">157</i>&nbsp;     */
<i class="no-highlight">158</i>&nbsp;    protected void addSinglePlayerKeys(final PacManUiBuilder builder) {
<b class="nc"><i class="no-highlight">159</i>&nbsp;        builder.addKey(KeyEvent.VK_UP, moveTowardsDirection(Direction.NORTH))</b>
<b class="nc"><i class="no-highlight">160</i>&nbsp;                .addKey(KeyEvent.VK_DOWN, moveTowardsDirection(Direction.SOUTH))</b>
<b class="nc"><i class="no-highlight">161</i>&nbsp;                .addKey(KeyEvent.VK_LEFT, moveTowardsDirection(Direction.WEST))</b>
<b class="nc"><i class="no-highlight">162</i>&nbsp;                .addKey(KeyEvent.VK_RIGHT, moveTowardsDirection(Direction.EAST));</b>
<i class="no-highlight">163</i>&nbsp;    }
<i class="no-highlight">164</i>&nbsp;
<i class="no-highlight">165</i>&nbsp;    private Action moveTowardsDirection(Direction direction) {
<b class="nc"><i class="no-highlight">166</i>&nbsp;        return () -&gt; {</b>
<b class="nc"><i class="no-highlight">167</i>&nbsp;            assert game != null;</b>
<b class="nc"><i class="no-highlight">168</i>&nbsp;            getGame().move(getSinglePlayer(getGame()), direction);</b>
<i class="no-highlight">169</i>&nbsp;        };
<i class="no-highlight">170</i>&nbsp;    }
<i class="no-highlight">171</i>&nbsp;
<i class="no-highlight">172</i>&nbsp;    private Player getSinglePlayer(final Game game) {
<b class="nc"><i class="no-highlight">173</i>&nbsp;        List&lt;Player&gt; players = game.getPlayers();</b>
<b class="nc"><i class="no-highlight">174</i>&nbsp;        if (players.isEmpty()) {</b>
<b class="nc"><i class="no-highlight">175</i>&nbsp;            throw new IllegalArgumentException(&quot;Game has 0 players.&quot;);</b>
<i class="no-highlight">176</i>&nbsp;        }
<b class="nc"><i class="no-highlight">177</i>&nbsp;        return players.get(0);</b>
<i class="no-highlight">178</i>&nbsp;    }
<i class="no-highlight">179</i>&nbsp;
<i class="no-highlight">180</i>&nbsp;    /**
<i class="no-highlight">181</i>&nbsp;     * Creates and starts a JPac-Man game.
<i class="no-highlight">182</i>&nbsp;     */
<i class="no-highlight">183</i>&nbsp;    public void launch() {
<b class="nc"><i class="no-highlight">184</i>&nbsp;        makeGame();</b>
<b class="nc"><i class="no-highlight">185</i>&nbsp;        PacManUiBuilder builder = new PacManUiBuilder().withDefaultButtons();</b>
<b class="nc"><i class="no-highlight">186</i>&nbsp;        addSinglePlayerKeys(builder);</b>
<b class="nc"><i class="no-highlight">187</i>&nbsp;        pacManUI = builder.build(getGame());</b>
<b class="nc"><i class="no-highlight">188</i>&nbsp;        pacManUI.start();</b>
<i class="no-highlight">189</i>&nbsp;    }
<i class="no-highlight">190</i>&nbsp;
<i class="no-highlight">191</i>&nbsp;    /**
<i class="no-highlight">192</i>&nbsp;     * Disposes of the UI. For more information see
<i class="no-highlight">193</i>&nbsp;     * {@link javax.swing.JFrame#dispose()}.
<i class="no-highlight">194</i>&nbsp;     *
<i class="no-highlight">195</i>&nbsp;     * Precondition: The game was launched first.
<i class="no-highlight">196</i>&nbsp;     */
<i class="no-highlight">197</i>&nbsp;    public void dispose() {
<b class="nc"><i class="no-highlight">198</i>&nbsp;        assert pacManUI != null;</b>
<b class="nc"><i class="no-highlight">199</i>&nbsp;        pacManUI.dispose();</b>
<i class="no-highlight">200</i>&nbsp;    }
<i class="no-highlight">201</i>&nbsp;
<i class="no-highlight">202</i>&nbsp;    /**
<i class="no-highlight">203</i>&nbsp;     * Main execution method for the Launcher.
<i class="no-highlight">204</i>&nbsp;     *
<i class="no-highlight">205</i>&nbsp;     * @param args
<i class="no-highlight">206</i>&nbsp;     *            The command line arguments - which are ignored.
<i class="no-highlight">207</i>&nbsp;     * @throws IOException
<i class="no-highlight">208</i>&nbsp;     *             When a resource could not be read.
<i class="no-highlight">209</i>&nbsp;     */
<i class="no-highlight">210</i>&nbsp;    public static void main(String[] args) throws IOException {
<b class="nc"><i class="no-highlight">211</i>&nbsp;        new Launcher().launch();</b>
<i class="no-highlight">212</i>&nbsp;    }
<i class="no-highlight">213</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-05-22 18:55</div>
</div>
</body>
</html>
